package com.symone.webcms.security;

import com.symone.webcms.common.Constants;
import com.symone.webcms.model.Menu;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class RequestInterceptor extends HandlerInterceptorAdapter {

    protected Logger logger = LoggerFactory.getLogger(RequestInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("Interceptor preHandler method is running !");
        String paramString = request.getQueryString();
        String uri = request.getRequestURI();
        if(paramString != null){
            uri += "?" + paramString;
        }
        setCurrentMenuIds(uri);
        logger.info("URL:" + uri );
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.debug("Interceptor postHandler method is running !");
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("Interceptor afterCompletion method is running !");
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("Interceptor afterConcurrentHandlingStarted method is running !");
        super.afterConcurrentHandlingStarted(request, response, handler);
    }

    private void setCurrentMenuIds(String uri){
        if(uri.contains("error")){
            return;
        }
        Session session = SecurityUtils.getSubject().getSession();
        List<Menu> menus = (List<Menu>)session.getAttribute(Constants.CURRENT_MENU_LIST);
        if(menus == null || menus.size() == 0){
            return;
        }
        for (Menu menu : menus){
            if(menu == null || menu.getUrl() == null){
                return;
            }else if(menu.getUrl().equals(uri)){
                session.setAttribute(Constants.CURRENT_USER_MENU_IDS,menu.getId());
                return;
            }else {
                if(menu.getChildren() != null && menu.getChildren().size() > 0){
                    for(Menu child : menu.getChildren()){
                        if(child.getUrl().equals(uri)){
                            session.setAttribute(Constants.CURRENT_USER_MENU_IDS,menu.getId()+","+child.getId());
                            return;
                        }
                    }
                }
            }
        }
        session.setAttribute(Constants.CURRENT_USER_MENU_IDS,1);
    }

}